<?php
namespace App\Http\Controllers\Laifei\Talent;

use App\Http\Controllers\Controller;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaifeiTalentStoreController extends Controller
{

    public function index()
    {

        $all = $this->all;
        $pageSize = $all['pageSize'] ?? 20;
        $pageNo = $all['pageNo'] ?? 1;


        try{

            $where = function ($query) use ($all) {
                if(isset($all['talent_id'])){
                    $query->where('t.talent_id','=',$all['talent_id']);
                }
                if(isset($all['nickname'])){
                    $query->where('lu.nickname','like','%'.$all['nickname'].'%');
                }
                if(isset($all['openid'])){
                    $query->where('lu.openid','=',$all['openid']);
                }
                if(isset($all['account_name'])){
                    $query->where('t.account_name','like','%'.$all['account_name'].'%');
                }
                if(isset($all['channel_id']) && count($all['channel_id'])){
                    $query->whereIn('t.channel_id',$all['channel_id']);
                }
                if(isset($all['status']) && count($all['status'])){
                    $query->whereIn('t.status',$all['status']);
                }
                if(isset($all['verify_time']) && count($all['verify_time'])){
                    $query->whereBetween('t.verify_time',$all['verify_time']);
                }
            };

            $total = DB::table('talent as t')
                ->leftJoin('lottery_users as lu','t.we_id','=','lu.we_id')
                ->where($where)
                ->count();

            $info = DB::table('talent as t')
                ->leftJoin('lottery_users as lu','t.we_id','=','lu.we_id')
                ->leftJoin('explore_channel as ec','t.channel_id','=','ec.channel_id')
                ->leftJoin('dict_area as da','t.district','=','da.id')
                ->where($where)
                ->select('t.talent_id','t.we_id','t.apply_time','t.channel_id','t.account_name','t.verify_img',
                    't.status','t.intelligent','t.verify_time','t.verify_user','t.remark','t.province','t.city','t.district',
                    'da.short_name','lu.openid','lu.nickname','lu.phone as phone','ec.channel_name')
                ->orderByDesc('t.talent_id')
                ->skip(($pageNo - 1) * $pageSize)
                ->take($pageSize)
                ->get()
                ->map(function($item){
                    $item->status_name = Data::$talentStatus[$item->status];
                    return $item;
                })
                ->toArray();

            return $this->setReturnJson(0,'success',['total'=>$total,'info'=>$info]);

        }catch (\Exception $e){
            Log::error(LaifeiTalentStoreController::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'查询失败');
        }


    }

}
